package com.zzyl.nursing.service.impl;

import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzyl.common.constant.HttpStatus;
import com.zzyl.common.core.domain.model.LoginUser;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.common.utils.UserThreadLocal;
import com.zzyl.nursing.domain.NursingPlan;
import com.zzyl.nursing.dto.NursingPageDTO;
import com.zzyl.nursing.vo.NursingProjectVo;
import io.swagger.util.Yaml;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.NursingProjectMapper;
import com.zzyl.nursing.domain.NursingProject;
import com.zzyl.nursing.service.INursingProjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;

/**
 * 护理项目Service业务层处理
 *
 * @author alexis
 * @date 2024-12-30
 */
@Service
public class NursingProjectServiceImpl extends ServiceImpl<NursingProjectMapper, NursingProject> implements INursingProjectService {
    @Autowired
    private NursingProjectMapper nursingProjectMapper;

    @Resource
    private RedisTemplate<Object, Object> redisTemplate;
    private static final String CACHE_KEY_PREFIX = "nursingProject:all";

    /**
     * 查询护理项目
     *
     * @param id 护理项目主键
     * @return 护理项目
     */
    @Override
    public NursingProject selectNursingProjectById(Long id) {
        return nursingProjectMapper.selectById(id);
    }

    /**
     * 查询护理项目列表
     *
     * @param nursingProject 护理项目
     * @return 护理项目
     */
    @Override
    public List<NursingProject> selectNursingProjectList(NursingProject nursingProject) {
        return nursingProjectMapper.selectNursingProjectList(nursingProject);
    }

    /**
     * 新增护理项目
     *
     * @param nursingProject 护理项目
     * @return 结果
     */
    @Override
    public int insertNursingProject(NursingProject nursingProject) {
        int i = nursingProjectMapper.insert(nursingProject);
        deleteCache();
        return i;
    }

    /**
     * 修改护理项目
     *
     * @param nursingProject 护理项目
     * @return 结果
     */
    @Override
    public int updateNursingProject(NursingProject nursingProject) {
        int i = nursingProjectMapper.updateById(nursingProject);
        deleteCache();
        return i;
    }

    /**
     * 批量删除护理项目
     *
     * @param ids 需要删除的护理项目主键
     * @return 结果
     */
    @Override
    public int deleteNursingProjectByIds(Long[] ids) {
        int i = nursingProjectMapper.deleteBatchIds(Arrays.asList(ids));
        deleteCache();
        return i;
    }

    /**
     * 删除护理项目信息
     *
     * @param id 护理项目主键
     * @return 结果
     */
    @Override
    public int deleteNursingProjectById(Long id) {
        int i = nursingProjectMapper.deleteById(id);
        deleteCache();
        return i;
    }

    private void deleteCache() {
        redisTemplate.delete(CACHE_KEY_PREFIX);
    }

    /**
     * 查询所有护理项目VO列表
     *
     * @return 结果
     */
    @Override
    public List<NursingProjectVo> getAllProjects() {
        List<NursingProjectVo> list = (List<NursingProjectVo>) redisTemplate.opsForValue().get(CACHE_KEY_PREFIX);
        if (CollectionUtils.isNotEmpty(list)) {
            return list;
        }

        list = nursingProjectMapper.getAllProjects();
        redisTemplate.opsForValue().set(CACHE_KEY_PREFIX, list);

        return list;
    }

    /**
     * 分页查询护理项目列表
     *
     * @param nursingPageDTO
     * @return
     */
    @Override
    public TableDataInfo pageProject(NursingPageDTO nursingPageDTO) {
        Page<NursingProject> page = new Page<>(nursingPageDTO.getPageNum(), nursingPageDTO.getPageSize());
        LambdaQueryWrapper<NursingProject> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(NursingProject::getName, nursingPageDTO.getName())
                .eq(NursingProject::getStatus, nursingPageDTO.getStatus());
        page = page(page, wrapper);

        TableDataInfo<NursingProject> tableDataInfo = new TableDataInfo<>(page.getRecords(), page.getTotal());
        tableDataInfo.setCode(HttpStatus.SUCCESS);
        tableDataInfo.setMsg("查询成功");
        return tableDataInfo;
    }

}
